home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS14.ADF / Tool / display.c < prev    next >
C/C++ Source or Header  |  1989-01-28  |  12KB  |  445 lines

  1. /*
  2.    DISPLAY.C  Copyright Daniel D. Kary 1986
  3.    Standard Listing
  4. */
  5. #include <exec/types.h>
  6. #include <exec/tasks.h>
  7. #include <exec/libraries.h>
  8. #include <exec/devices.h>
  9. #include <graphics/copper.h>
  10. #include <graphics/display.h>
  11. #include <graphics/text.h>
  12. #include <graphics/view.h>
  13. #include <graphics/gels.h>
  14. #include <graphics/regions.h>
  15. #include <hardware/blit.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/intuitionbase.h>
  18. #include <stdio.h>
  19. #include <libraries/dos.h>
  20. #include <workbench/workbench.h>
  21.  
  22. /* Resource Flag definitions */
  23. #define F_INTUITION     0x000001
  24. #define F_GRAPHICS      0x000002
  25. #define F_WRITE_PORT    0x000004
  26. #define F_WRITE_MSG     0x000008
  27. #define F_WINDOW        0x000010
  28. #define F_CONSOLE       0x000020
  29.  
  30. /* Menu definitions */
  31. #define MENU_PROJECT    0
  32. #define ITEM_HEIGHT     10
  33.  
  34. /* for the PROJECT Menu ... */
  35. #define PROJECT_TOP     0
  36. #define PROJECT_BOTTOM  1
  37. #define PROJECT_UP      2
  38. #define PROJECT_DOWN    3
  39. #define PROJECT_WIDTH   120
  40. #define PROJECT_BAR     75
  41.  
  42. /* for the DATA Menu's ... */
  43. #define DATA_BAR        40
  44. #define DATA_WIDTH      95
  45. #define DATA_LEFT       (-60)
  46. #define MAXHEADINGS     14
  47. #define MAXLINES        18
  48. #define MAXITEMS        (MAXHEADINGS * MAXLINES)
  49.  
  50. struct Text {
  51.                /* linked list of lines of text */
  52.       struct   Text *next;
  53.       char     *line;
  54.    };
  55.  
  56. struct X_MenuItem {
  57. /* expanded MenuItem, Includes a pointer to user data */
  58.       struct   X_MenuItem *NextItem;
  59.       SHORT    LeftEdge, TopEdge;
  60.       SHORT    Width, Height;
  61.       USHORT   Flags;
  62.       LONG     MutEx;
  63.       APTR     ItemFill;
  64.       APTR     SFill;
  65.       BYTE     Command;
  66.       struct   MenuItem *SubItem;
  67.       USHORT   Select;
  68.       struct   Text *text;
  69.    };
  70.  
  71.  
  72. struct IntuiText ProjectText =
  73.    {
  74.       0, 1, JAM1,             /* front, back, mode */
  75.       1, 1,                   /* left, top */
  76.       NULL,                   /* font */
  77.       "RE-DISPLAY",
  78.       NULL,                   /* next */
  79.    };
  80.  
  81. struct MenuItem Project =
  82.    {
  83.       NULL,                   /* next */
  84.       0, ITEM_HEIGHT * 0,     /* select box left, top */
  85.       PROJECT_WIDTH,          /* select box width */
  86.       ITEM_HEIGHT,            /* select box height */
  87.       ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP,
  88.                               /* flags */
  89.       0,                      /* mutual exclude */
  90.       (APTR)&ProjectText,     /* text */
  91.       NULL,                   /* select image */
  92.       'R',                    /* command */
  93.       NULL,                   /* subitem */
  94.       NULL,                   /* next select */
  95.    };
  96.  
  97. struct Menu MainMenu =
  98.    {
  99.       NULL,                   /* next menu */
  100.       5, 0,                   /* select left, top */
  101.       75, 10,                 /* select width, height */
  102.       MENUENABLED,            /* flags */
  103.       "Project",              /* text */
  104.       &Project,               /* first item */
  105.       0, 0, 0, 0,             /* mystery variables */
  106.    };
  107.  
  108. struct NewWindow nw =
  109.    {
  110.       0,0,                    /* left, top */
  111.       640,200,                /* width, height */
  112.       -1,-1,                  /* detail pen, block pen */
  113.       CLOSEWINDOW | MOUSEBUTTONS  | MENUPICK,
  114.                               /* IDCMP Flags */
  115.       WINDOWDEPTH | WINDOWSIZING  | WINDOWDRAG |
  116.       ACTIVATE    | SMART_REFRESH | WINDOWCLOSE,
  117.                               /* gadget flags */
  118.       NULL,                   /* user gadgets */
  119.       NULL,                   /* user check   */
  120.       "Structure Display",    /* title */
  121.       NULL,                   /* screen */
  122.       NULL,                   /* super bitmap */
  123.       100,45,                 /* min width, min height */
  124.       640,200,                /* max width, max height */
  125.       WBENCHSCREEN            /* screen type */
  126.    };
  127.  
  128. struct X_MenuItem X_MenuTemplate =
  129.    {
  130.       NULL,                   /* next */
  131.       DATA_LEFT, 0,           /* left, top */
  132.       DATA_WIDTH, ITEM_HEIGHT,/* width, height */
  133.       ITEMTEXT | ITEMENABLED | HIGHCOMP,
  134.                               /* flags */
  135.       0, NULL, NULL,          /* MutEx, Fills */
  136.       ' ', NULL,              /* Command, subitem */
  137.       NULL, NULL              /* Next, Text List */
  138.    };
  139.  
  140. struct Menu MenuTemplate =
  141.    {
  142.       NULL,                   /* next */
  143.       0, 0,                   /* left, top */
  144.       DATA_BAR, ITEM_HEIGHT,  /* width, height */
  145.       MENUENABLED,            /* flags */
  146.       NULL, NULL,             /* text, first item */
  147.       0,0,0,0                 /* mystery variables */
  148.    };
  149.  
  150. extern  struct  IOStdReq  *CreateStdIO();
  151. extern  struct  MsgPort   *CreatePort();
  152.  
  153. struct  Window       *w;
  154. struct  IntuiMessage *message;
  155. struct  IOStdReq     *WriteMsg;
  156. struct  MsgPort      *WritePort;
  157. struct  Text         *current_text = NULL;
  158. struct  X_MenuItem   *head  = NULL;
  159. struct  X_MenuItem   *temp  = NULL;
  160. struct  Text         *first = NULL;
  161. struct  Text         *t     = NULL;
  162. int     Items = 0;
  163. ULONG   GfxBase;
  164. ULONG   IntuitionBase;
  165. int     cur_resource = NULL;
  166.  
  167.  
  168. main(argc, argv)
  169. int argc;
  170. char *argv[];
  171. {
  172.    int i, class, code;
  173.    struct X_MenuItem *p;
  174.  
  175.    if(argc < 2){
  176.       printf("USAGE: %s file(s)\n", argv[0]);
  177.       exit(1);
  178.    }
  179.    if((GfxBase = OpenLibrary("graphics.library",0)) == NULL)
  180.       abort("Can't open graphics.library");
  181.    cur_resource |= F_GRAPHICS;
  182.  
  183.    if((IntuitionBase =
  184.       OpenLibrary("intuition.library",0)) == NULL)
  185.       abort("Can't open intuition.library");
  186.    cur_resource |= F_INTUITION;
  187.  
  188.    if((WritePort = CreatePort("con.write",0)) == 0)
  189.       abort("Can't create console write port");
  190.    cur_resource |= F_WRITE_PORT;
  191.  
  192.    if((WriteMsg = CreateStdIO(WritePort)) == 0)
  193.       abort("Can't create console write message");
  194.    cur_resource |= F_WRITE_MSG;
  195.  
  196.    if((w = (struct Window *) OpenWindow(&nw)) == NULL)
  197.       abort("Can't open new window");
  198.    cur_resource |= F_WINDOW;
  199.  
  200.    WriteMsg->io_Data = (APTR) w;
  201.    WriteMsg->io_Length = sizeof(*w);
  202.    if((OpenDevice("console.device", 0, WriteMsg, 0)) != 0)
  203.       abort("Can't open console");
  204.    cur_resource |= F_CONSOLE;
  205.  
  206.    PutStr(WriteMsg,"\033[0 p");    /* turn the cursor off */
  207.    PutStr(WriteMsg, "Reading Data File(s), Please Wait.\n");
  208.    for(i = 1; i < argc; i++)
  209.       read_file(argv[i]);
  210.    build_menu();
  211.    PutChar(WriteMsg, 0x0c);           /* clear the screen */
  212.    SetMenuStrip(w, &MainMenu);
  213.  
  214.    while(1){
  215.       Wait(-1);
  216.       while(message = (struct IntuiMessage *)
  217.                       GetMsg(w->UserPort)){
  218.          class = message->Class;
  219.          code  = message->Code;
  220.          ReplyMsg(message);
  221.          if(class == CLOSEWINDOW){
  222.             abort("Bye!");
  223.          }
  224.          if(class == MENUPICK){
  225.             switch MENUNUM(code){
  226.                case NOMENU:
  227.                    break;
  228.                case MENU_PROJECT:
  229.                    redisplay();
  230.                    break;
  231.                default:
  232.                    p = (struct X_MenuItem *)
  233.                        ItemAddress(&MainMenu, code);
  234.                    current_text = p->text;
  235.                    redisplay();
  236.                    break;
  237.             }
  238.          }
  239.       }     
  240.    }
  241. }
  242.  
  243. abort(s)
  244. char *s;
  245. {
  246.    if (cur_resource & F_CONSOLE)
  247.       CloseDevice(WriteMsg);
  248.    if (cur_resource & F_WINDOW){
  249.             ClearMenuStrip(w);
  250.             CloseWindow(w);
  251.    }
  252.    if (cur_resource & F_WRITE_MSG)
  253.       DeleteStdIO(WriteMsg);
  254.    if (cur_resource & F_WRITE_PORT)
  255.       DeletePort(WritePort);
  256.    if (cur_resource & F_GRAPHICS)
  257.       CloseLibrary(GfxBase);
  258.    if (cur_resource & F_INTUITION)
  259.       CloseLibrary(IntuitionBase);
  260.    printf("%s\n",s);
  261.    exit(0);
  262. }
  263.  
  264. redisplay()
  265. {
  266.    int i, class, code;
  267.    struct Text *t;
  268.  
  269.    i = 0;
  270.    t = current_text;
  271.    PutChar(WriteMsg, 0x0c);          /* clear the screen */
  272.    while(t){
  273.       if(message = (struct IntuiMessage *)
  274.                    GetMsg(w->UserPort)){
  275.          class = message->Class;
  276.          code  = message->Code;
  277.          ReplyMsg(message);
  278.          if(class == MOUSEBUTTONS)
  279.             if(code == SELECTDOWN)
  280.                return(0);
  281.       }
  282.       PutStr(WriteMsg, t->line);
  283.       t = t->next;
  284.       if(i++ > 16) Delay(3);
  285.    }
  286. }
  287.  
  288. read_file(s)
  289. char *s;
  290. {
  291.    FILE *fp;
  292.    char buf[256];
  293.    struct IntuiText *it;
  294.  
  295.    if((fp = fopen(s, "r")) == NULL){
  296.       sprintf(&buf[0],"Unable to read %s\n", s);
  297.       PutStr(WriteMsg, &buf[0]);
  298.       Delay(120);
  299.       return(0);
  300.    }
  301.    while((fgets(&buf[0], 256, fp)) != NULL){
  302.       if(buf[0] == '!'){
  303.          buf[(strlen(&buf[0])-1)] = '\0';
  304.          if(Items == MAXITEMS){
  305.             sprintf(&buf[0],
  306.                     "MAXITEMS exceeded in %s\r\n", s);
  307.             PutStr(WriteMsg, &buf[0]);
  308.             fclose(fp);
  309.             Delay(120);
  310.             return(0);
  311.          }
  312.          Items++;
  313.          temp = (struct X_MenuItem *)
  314.                 myalloc(sizeof(struct X_MenuItem));
  315.          *temp = X_MenuTemplate;
  316.          temp->ItemFill = (APTR)
  317.                           myalloc(sizeof(struct IntuiText));
  318.          it = (struct IntuiText *) temp->ItemFill;
  319.          *it = ProjectText;
  320.          it->IText = (UBYTE *) myalloc(12);
  321.          first = NULL;
  322.          strncpy(it->IText, &buf[1], 11);
  323.          insert(temp);
  324.       }
  325.       else if(temp){
  326.          t = (struct Text *) myalloc(sizeof(struct Text));
  327.          t->line = (char *) myalloc(strlen(&buf[0])+1);
  328.          strcpy(t->line, &buf[0]);
  329.          t->next = NULL;
  330.          if(first)
  331.             first->next = t;
  332.          else
  333.             temp->text = t;
  334.          first = t;
  335.       }
  336.    }
  337.    fclose(fp);
  338.    temp  = NULL;
  339.    first = NULL;
  340. }
  341.  
  342. build_menu()
  343. {
  344.    int  i, j, headings, items;
  345.    struct Menu *m;
  346.    struct IntuiText *t;
  347.  
  348.    if(Items <= MAXHEADINGS){
  349.       headings = Items;
  350.       items = 1;
  351.    }
  352.    else{
  353.       headings = MAXHEADINGS;
  354.       items = Items/(MAXHEADINGS - 1);
  355.    }
  356.    m = &MainMenu;
  357.    for(i = 0; i < headings; i++){
  358.       if(head){
  359.          m->NextMenu = (struct Menu *)
  360.                        myalloc(sizeof(struct Menu));
  361.          m = m->NextMenu;
  362.          *m = MenuTemplate;
  363.          m->LeftEdge = PROJECT_BAR + (DATA_BAR * i);
  364.          m->MenuName = (BYTE *) myalloc(4);
  365.          m->FirstItem = (struct MenuItem *) head;
  366.          t = (struct IntuiText *) head->ItemFill;
  367.          strncpy(m->MenuName, t->IText, 3);
  368.       }
  369.       else return(0);
  370.       if(i == (headings - 1))
  371.          items = MAXLINES;
  372.       for(j = 0; j < items; j++){
  373.          head->TopEdge = ITEM_HEIGHT * j;
  374.          temp = head;
  375.          head = head->NextItem;
  376.          if(head == NULL)
  377.             return(0);
  378.          if(j == (items - 1))
  379.             temp->NextItem = NULL;
  380.       }
  381.    }
  382. }
  383.  
  384. insert(p)
  385. struct X_MenuItem *p;
  386. {
  387.    struct X_MenuItem *t1, *t2;
  388.    struct IntuiText  *s1, *s2;
  389.  
  390.    s1 = (struct IntuiText *) p->ItemFill;
  391.    if(head){
  392.       t1 = head;
  393.       t2 = NULL;
  394.       while(t1){
  395.          s2 = (struct IntuiText *) t1->ItemFill;
  396.          if((strcmp(s1->IText, s2->IText)) <= 0){
  397.             p->NextItem  = t1;
  398.             if(t2 == NULL)
  399.                head = p;
  400.             else
  401.                t2->NextItem = p;
  402.             return(0);
  403.          }
  404.          t2 = t1;
  405.          t1 = t1->NextItem;
  406.       }
  407.       t2->NextItem = p;
  408.    }
  409.    else
  410.       head = p;
  411. }
  412.  
  413. PutStr(r, s)
  414. struct IOStdReq *r;
  415. char   *s;
  416. {
  417.    r->io_Command = CMD_WRITE;
  418.    r->io_Data    = (APTR) s;
  419.    r->io_Length  = -1;
  420.    DoIO(r);
  421.    return(0);
  422. }
  423.  
  424. PutChar(r, c)
  425. struct IOStdReq *r;
  426. char   c;
  427. {
  428.    r->io_Command = CMD_WRITE;
  429.    r->io_Data    = (APTR) &c;
  430.    r->io_Length  = 1;
  431.    DoIO(r);
  432.    return(0);
  433. }
  434.  
  435. myalloc(n)
  436. int n;
  437. {
  438.    int i;
  439.  
  440.    i = malloc(n);
  441.    if(i == 0)
  442.       abort("Insufficient Memory");
  443.    return(i);
  444. }
  445.